<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Tìm Đường Đi</title>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/inputClient.js"></script>
<script type="text/javascript" src="js/algorithm_noheap.js"></script>
<script type="text/javascript" src="js/showTextPath.js"></script>

<?php
	include '/../mdSta/data.php';
	$data = new Data();
?>
<?php
	  include '/../dataStation/dataCal.php';
	  $allStation = loadData();
?>
</head>

<body>
	Số lần chạy :
	<input id="counts" type="number">
    Số mục tiêu :
    <select id="priority">
    	<option value="1" selected>1</option>
        <option value="2">2</option>
        <option value="3">3</option>
    </select>
    Lựa chọn đi bộ :
    <select id="walk">
    	<option value="swalk" selected>Có</option>
        <option value="snowalk">Không</option>
    </select>
    <input id="oncewalk" type="number" value="200">
    <input id="maxwalk" type="number" value="400">
    <button type="button" onClick="cal()">Start</button>

<h3>Y = Có Đi Bộ <br/> N = Không Đi Bộ</h3>
<h2>Thống kê chung</h2>
    <table width="1000" border="1" cellpadding="0" cellspacing="0" style="margin:20px 0px">
	  <tr>
	    <td width="200" align="center">Số lần chạy</td>
	    <td width="200" align="center">Tổng Thời gian(s)</td>
	    <td width="200" align="center">Thời gian trung bình(s)</td>
	    <td width="200" align="center">Tổng số Node duyệt qua</td>
	    <td width="200" align="center">Trung bình số Node duyệt qua</td>
	  </tr>
	  <tr>
	    <td id="1" align="center">&nbsp;</td>
	    <td id="2" align="center">&nbsp;</td>
	    <td id="3" align="center">&nbsp;</td>
	    <td id="4" align="center">&nbsp;</td>
	    <td id="5" align="center">&nbsp;</td>
	  </tr>
	  <tr>
	    <td id="6" align="center">&nbsp;</td>
	    <td id="7" align="center">&nbsp;</td>
	    <td id="8" align="center">&nbsp;</td>
	    <td id="9" align="center">&nbsp;</td>
	    <td id="10" align="center">&nbsp;</td>
	  </tr>
	</table>

<h2>Thống kê 10 lần chạy đầu</h2>
<div id="five">

</div>

</body>
<script type="text/javascript">
	$('#walk').change(function(e) {
		var optionwalk = $('#walk').val();
        if(optionwalk == 'swalk')
		{
			$('#oncewalk').show();
			$('#maxwalk').show();
		}
		else
		{
			$('#oncewalk').hide();
			$('#maxwalk').hide();
		}
    });
	var dataAllS = <?php echo json_encode($allStation);?>;
</script>
<script type="text/javascript">
	// ---------- five show ---------------------------------------------------------
	var dS = <?php echo json_encode($data->resultData('station'));?>;
	// ------------------------------------------------------------------------------
	var onePri = <?php echo json_encode($onePri);?>;
	var twoPri = <?php echo json_encode($twoPri);?>;
	var threePri = <?php echo json_encode($threePri);?>;
	function cal()
	{
		// ----- five ----------------------------
		var fiveShow = '';
		fiveShow = fiveShow + '<table width="1000" border="1" cellpadding="0" cellspacing="0" style="margin:20px 0px">';

		fiveShow = fiveShow + '<tr><td width="250" align="center">Điểm bắt đầu</td>';
		fiveShow = fiveShow + '<td width="250" align="center">Điểm đến</td>';
		fiveShow = fiveShow + '<td width="250" align="center">Thời gian thực thi giải thuật(ms)</td>';
		fiveShow = fiveShow + '<td width="250" align="center">Số đỉnh duyệt qua</td></tr>';
		// ---------------------------------------
		var numRunY = parseInt($('#counts').val());
		var prioInput;
		var optionW = optionwalk();
		var sumtimeY = 0;
		var sumNodeY = 0;
		var sumtimeN = 0;
		var sumNodeN = 0;
		for(var i = 0; i < numRunY; i++)
		{
			prioInput = [];
			var setInput = [];
			setInput.push(dataAllS[i].source);
			setInput.push(dataAllS[i].target);
			console.log(setInput, i) // OK

			prioInput = prio(i);
			for(var j = 0; j < prioInput.length; j++)
			{
				setInput.push(prioInput[j]);
			}
			// ================= Excution time =============
			var resultY = calAl(setInput, optionW);
			sumtimeY = sumtimeY + resultY[0];

			sumNodeY = sumNodeY + resultY[1].length;

			optionN = [];
			var resultN = calAl(setInput, optionN);
			sumtimeN = sumtimeN + resultN[0];

			sumNodeN = sumNodeN + resultN[1].length;

			if(i < 10)
			{
				fiveShow = fiveShow + '<tr><td align="center">' + dS[dataAllS[i].source].name +'</td>';
				fiveShow = fiveShow + '<td align="center">' + dS[dataAllS[i].target].name +'</td>';
				fiveShow = fiveShow + '<td align="center">' + 'Y : ' + resultY[0] + '<br/> N : ' + resultN[0] + '</td>';
				fiveShow = fiveShow + '<td align="center">' + 'Y : ' + resultY[1].length + '<br/> N : ' + resultN[1].length + '</td></tr>';
			}
		}
		sumtimeY = sumtimeY/1000;
		sumtimeN = sumtimeN/1000;
		$('#1').html(numRunY + ' (Y)');
		$('#2').html(sumtimeY);
		$('#3').html(sumtimeY/numRunY);
		$('#4').html(sumNodeY);
		$('#5').html(sumNodeY/numRunY);

		$('#6').html(numRunY + ' (N)');
		$('#7').html(sumtimeN);
		$('#8').html(sumtimeN/numRunY);
		$('#9').html(sumNodeN);
		$('#10').html(sumNodeN/numRunY);

		// ---------- five show -------------------------------------------------
		fiveShow = fiveShow + '</table>';
		$('#five').html(fiveShow);
	}

	function calAl(setInput, optionW)
	{
		//console.log('optionW', optionW);
		//console.log('setInput', setInput);
		//
		var result = [];
		var dNext = <?php echo json_encode($data->resultData('nextS'));?>;

		var dNear = <?php echo json_encode($data->resultData('nearS'));?>;

		var start = new Date().getTime();

		setData(dNext, dNear, setInput, optionW);
		var resultAlgorithm = shortTestPath();
		var end = new Date().getTime();
		var time = end - start;
		result.push(time);
		result.push(resultAlgorithm);
		return result;
	}

	function prio(i)
	{
		var numprio = parseInt($('#priority').val());
		if(numprio == 1)
		{
			var min=0, max=2
			var rand = min + Math.random()*(max+1-min)
			rand = Math.floor(rand)
			return onePri[rand];
		}
		else if(numprio == 2)
		{
			var min=0, max=5
			var rand = min + Math.random()*(max+1-min)
			rand = Math.floor(rand)
			return twoPri[rand];
		}
		else
		{
			var min=0, max=5
			var rand = min + Math.random()*(max+1-min)
			rand = Math.floor(rand)
			return threePri[rand];
		}
	}
	function optionwalk()
	{
		var datawalk = [];
		var opt = $('#walk').val();
		if(opt == 'swalk')
		{
			datawalk.push($('#oncewalk').val());
			datawalk.push($('#maxwalk').val());
		}
		return datawalk;
	}
</script>
<script type="text/javascript">
</script>
</html>
